package com.kexio.core.data.config;

import java.time.Duration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.github.benmanes.caffeine.cache.Caffeine;

import jakarta.annotation.PostConstruct;

/**
 * 缓存配置类
 * 
 * 功能说明：
 * - 配置Spring Cache
 * - 配置Caffeine本地缓存
 * - 支持Redis分布式缓存（当Redis可用时）
 * 
 * @author Kexio
 * @since 1.0.0
 */
@Configuration
@EnableCaching
@ConditionalOnClass(CacheManager.class)
public class CacheConfig {

    private static final Logger log = LoggerFactory.getLogger(CacheConfig.class);

    @PostConstruct
    public void init() {
        log.info("==> Kexio缓存配置已启用");
        log.debug("缓存配置: Spring Cache + Caffeine本地缓存");
    }

    /**
     * Caffeine本地缓存管理器
     * 
     * 特性：
     * - 高性能本地缓存
     * - LRU淘汰策略
     * - 支持过期时间
     * - 适合单机或缓存穿透防护
     */
    @Bean
    @Primary
    @ConditionalOnMissingBean(CacheManager.class)
    @ConditionalOnClass(Caffeine.class)
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        
        // 配置Caffeine
        cacheManager.setCaffeine(Caffeine.newBuilder()
                // 初始容量
                .initialCapacity(100)
                // 最大容量
                .maximumSize(10000)
                // 写入后过期时间
                .expireAfterWrite(Duration.ofMinutes(30))
                // 访问后过期时间
                .expireAfterAccess(Duration.ofMinutes(10))
                // 启用统计
                .recordStats()
        );
        
        // 设置缓存名称
        cacheManager.setCacheNames(java.util.Arrays.asList("kexio-cache", "user-cache", "permission-cache", "config-cache"));
        
        log.debug("==> Caffeine缓存管理器已配置: 最大容量=10000, 写入过期=30分钟, 访问过期=10分钟");
        
        return cacheManager;
    }

    /**
     * 当Redis可用时，可以添加Redis缓存配置
     * 这里先注释掉，后续在需要时启用
     */
    /*
    @Bean
    @ConditionalOnClass(RedisTemplate.class)
    @ConditionalOnBean(RedisConnectionFactory.class)
    public RedisCacheManager redisCacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(60))
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                    .fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair
                    .fromSerializer(new GenericJackson2JsonRedisSerializer()));
        
        return RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
    }
    */
}
